import requests
import pytest
from api.ihrm_emploee_api import IhrmEmploeeApi
from common.assert_util import common_assert, common_put, common_delete, common_select
from common.get_id_util import get_id
from common.get_json_util import read_json_data
from config import IHRM_EMP_INSERT_JSON_DATA_PATH, MOBILE, IHRM_EMP_PUT_JSON_DATA_PATH, IHRM_EMP_DELETE_JSON_DATA_PATH, \
    IHRM_EMP_SELECT_JSON_DATA_PATH
from common.DBUtil import DBUtil


# 解决手机号码问题
# 1.随机：函数--->生成11位手机号的函数，去库里查一下，不存在的话直接使用，存在的话继续生成
# 2.测试之前，连接一下数据库，去库里把你准备好的手机号对应的数据，直接删除；
# 3.员工添加前，调用一下员工删除接口，把这个手机号对应的员工删除了

class TestIhrmEmploeeInsert:
    # 前后置
    def setup_method(self):
        # 链接数据库删除手机号
        DBUtil.uid_db(f"delete from bs_user where mobile={MOBILE}")

    def teardown_method(self):
        # 清楚垃圾数据
        DBUtil.uid_db(f"delete from bs_user where mobile={MOBILE}")

    # 员工添加成功
    @pytest.mark.parametrize("json_data,status_code,success,code,message",
                             read_json_data(IHRM_EMP_INSERT_JSON_DATA_PATH))
    def test_ihrm_emploee_insert(self, json_data, status_code, success, code, message):
        # 发送员工添加接口请求
        emp_insert_resp = IhrmEmploeeApi.ihrm_emploee_insert_api(json_data)
        # 断言
        common_assert(emp_insert_resp, status_code, success, code, message)

        # emp_insert_resp = requests.post(url="http://ihrm2-test.itheima.net/api/sys/user",
        #                                 headers={"Authorization": "token"},
        #                                 json={"username": "java之父007", "moblie": "18199009907",
        #                                       "workNumber": "9527"})

        # 断言
        # assert 200 == emp_insert_resp.status_code
        # assert True == emp_insert_resp.json().get("success")
        # assert 10000 == emp_insert_resp.json().get("code")
        # assert "操作成功" in emp_insert_resp.json().get("message")


class TestIhrmEmploeePut:

    @pytest.mark.parametrize("json_data,status_code,success,code,message", read_json_data(IHRM_EMP_PUT_JSON_DATA_PATH))
    def test_ihrm_emploee_put(self, json_data, status_code, success, code, message):
        # 发送员工修改接口请求
        emp_put_resp = IhrmEmploeeApi.ihrm_emploee_put_api(json_data)
        # 断言
        common_put(emp_put_resp, status_code, success, code, message)


class TestIhrmEmploeeDelete:

    @pytest.mark.parametrize("status_code,success,code,message", read_json_data(IHRM_EMP_DELETE_JSON_DATA_PATH))
    def test_ihrm_emploee_delete(self, status_code, success, code, message):
        # 发送员工删除接口请求
        emp_delete_resp = IhrmEmploeeApi.ihrm_emploee_delete_api()
        # 断言
        common_delete(emp_delete_resp, status_code, success, code, message)


class TestIhrmEmploeeselect:

    def teardown_method(self):
        DBUtil.uid_db(f"delete from bs_user where id = {get_id()}")

    @pytest.mark.parametrize("status_code,success,code,message", read_json_data(IHRM_EMP_SELECT_JSON_DATA_PATH))
    def test_ihrm_emploee_select(self, status_code, success, code, message):
        # 发送员工查询接口请求
        emp_select_resp = IhrmEmploeeApi.ihrm_emploee_select_api()
        # 断言
        common_select(emp_select_resp, status_code, success, code, message)


if __name__ == '__main__':
    pytest.main(["-s", "testIhrmEmploeel.py"])
